<template>
  <page-header-wrapper>
    <a-card :bordered="false">
      <a-row :gutter="24">
          <!-- 条件搜索 -->
          <div class="table-page-search-wrapper">
            <a-form layout="inline">
              <a-row :gutter="48">
                <a-col :md="6" :sm="24">
                  <a-form-item label="上课日期" >
                    <a-month-picker v-model="queryParam.date" allow-clear placeholder="请选择上课日期" @change="changeTime"/>
                  </a-form-item>
                </a-col>
                <a-col :md="6" :sm="24">
                  <a-form-item label="卡类型">
                    <a-select placeholder="请选择" v-model="queryParam.cardType" style="width: 100%" allow-clear>
                      <a-select-option v-for="(d, index) in statusOptions" :key="index" :value="d.dictValue">{{ d.dictLabel }}</a-select-option>
                    </a-select>
                  </a-form-item>
                </a-col>
                <a-col :md="6" :sm="24">
                  <a-form-item label="所在校区">
                    <a-tree-select
                      v-model="queryParam.deptId"
                      style="width: 100%"
                      :dropdown-style="{ maxHeight: '400px', overflow: 'auto' }"
                      :tree-data="deptOptions"
                      placeholder="请选择上课校区"
                      :replaceFields="{children:'children', title:'label', key:'id', value: 'id' }	"
                      tree-default-expand-all
                    >
                    </a-tree-select>
                  </a-form-item>
                </a-col>
                <a-col :md="6" :sm="24">
                  <a-form-item label="班型">
                    <a-select placeholder="请选择" v-model="queryParam.classType" style="width: 100%" allow-clear>
                      <a-select-option v-for="(d, index) in classOptions" :key="index" :value="d.dictValue">{{ d.dictLabel }}</a-select-option>
                    </a-select>
                  </a-form-item>
                </a-col>
              </a-row>
            </a-form>
          </div>
          <!-- 操作 -->
          <div class="table-operations">
            <a-button type="primary" @click="handleQuery"><a-icon type="search" />查询</a-button>
            <a-button type="primary" @click="handleExport">
              <a-icon type="download" />导出
            </a-button>
            <table-setting
              :style="{float: 'right'}"
              :table-size.sync="tableSize"
              v-model="columns"
              :refresh-loading="loading"
              @refresh="getList" />
          </div>
          <!-- 数据展示 -->
          <a-table
            :loading="loading"
            :size="tableSize"
            rowKey="id"
            :columns="columns"
            :data-source="list"
            :pagination="false"
            :bordered="tableBordered"
          >
            <span slot="startTime" slot-scope="text, record">
              {{ parseTime(record.startTime,'{y}-{m}-{d}') }}
            </span>
            <span slot="endTime" slot-scope="text, record">
              {{ parseTime(record.endTime,'{y}-{m}-{d}') }}
            </span>
            <span slot="classType" slot-scope="text, record">
              {{ classTypeFormat(record) }}
            </span>
          </a-table>
          <!-- 分页 -->
          <a-pagination
            class="ant-table-pagination"
            show-size-changer
            show-quick-jumper
            :current="queryParam.pageNum"
            :total="total"
            :page-size="queryParam.pageSize"
            :showTotal="total => `共 ${total} 条`"
            @showSizeChange="onShowSizeChange"
            @change="changeSize"
          />
      </a-row>
    </a-card>
  </page-header-wrapper>
</template>

<script>
import { incountReportPage } from '@/api/manager/studentCard'
import { tableMixin } from '@/store/table-mixin'
import { treeselect } from '@/api/system/dept'
import moment from 'moment'

export default {
  name: 'Report',
  mixins: [tableMixin],
  data () {
    return {
      selectOptions:[
        {key:1,value:1,name:'一月份'},
        {key:2,value:2,name:'二月份'},
        {key:3,value:3,name:'三月份'},
        {key:4,value:4,name:'四月份'},
        {key:5,value:5,name:'五月份'},
        {key:6,value:6,name:'六月份'},
        {key:7,value:7,name:'七月份'},
        {key:8,value:8,name:'八月份'},
        {key:9,value:9,name:'九月份'},
        {key:10,value:10,name:'十月份'},
        {key:11,value:11,name:'十一月份'},
        {key:12,value:12,name:'十二月份'},
      ],
      studentInfo:{},
      list: [],
      selectedRowKeys: [],
      selectedRows: [],
      // 高级搜索 展开/关闭
      advanced: false,
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      ids: [],
      loading: false,
      total: 0,
      // 班级类型字典
      scheduleTypeOptions: [],
      // 状态字典
      statusOptions: [],
      classOptions: [],
      // 部门树选项
      deptOptions: [{
        id: 0,
        label: '',
        children: []
      }],
      // 查询参数
      queryParam: {
        month: null,
        cardType: null,
        pageNum: 1,
        pageSize: 10,
        date:null,
      },
      columns: [
        {
          title: '校区',
          dataIndex: 'deptName',
          ellipsis: true,
          align: 'center'
        },
        {
          title: '学员',
          dataIndex: 'studentName',
          ellipsis: true,
          align: 'center'
        },{
          title: '班型',
          dataIndex: 'classType',
          ellipsis: true,
          scopedSlots: { customRender: 'classType' },
          align: 'center'
        },
        {
          title: '卡名称',
          dataIndex: 'cardName',
          ellipsis: true,
          align: 'center'
        },
        {
          title: '收款金额',
          dataIndex: 'chargeAmount',
          ellipsis: true,
          align: 'center'
        },
        {
          title: '卡开时间',
          dataIndex: 'startTime',
          ellipsis: true,
          scopedSlots: { customRender: 'startTime' },
          align: 'center'
        },
        {
          title: '结束时间',
          dataIndex: 'endTime',
          scopedSlots: { customRender: 'endTime' },
          ellipsis: true,
          align: 'center'
        },{
          title: '期初金额',
          dataIndex: 'beginPeriod',
          ellipsis: true,
          align: 'center'
        },{
          title: '期末金额',
          dataIndex: 'endPeriod',
          ellipsis: true,
          align: 'center'
        },{
          title: '课单价',
          dataIndex: 'unitPrice',
          ellipsis: true,
          align: 'center'
        }
      ]
    }
  },
  filters: {
  },
  created () {
    this.queryParam.date = moment().format('YYYY-MM') 
    this.queryParam.month = (moment().month() + 1) + "";
    this.getList()
    this.getTreeselect();
    this.getDicts('card_type').then(response => {
      this.statusOptions = response.data
    })
    
    this.getDicts('class_type').then(response => {
      this.classOptions = response.data
    })
  },
  computed: {
  },
  watch: {
  },
  methods: {
    changeTime(val){
      this.queryParam.date = val.format('YYYY-MM') 
    },
    classTypeFormat (row, column) {
      return this.selectDictLabel(this.classOptions, row.classType)
    },
    selectChange(val){
      this.queryParam.month = val.toString
    },
    /** 查询部门下拉树结构 */
    getTreeselect () {
      treeselect().then(response => {
        this.deptOptions = response.data
      })
    },
    timeChange(value,dateString){
      this.queryParam.startTime = dateString[0];
      this.queryParam.endTime = dateString[1];
    },
    /** 查询课程信息列表 */
    getList () {
      this.loading = true
      incountReportPage(this.queryParam).then(response => {
        this.list = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    /** 搜索按钮操作 */
    handleQuery () {
      this.queryParam.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery () {
      this.queryParam = {
        scheduleType: undefined,
        scheduleDate: undefined,
        start: undefined,
        end: undefined,
        teacherCount: undefined,
        studentCount: undefined,
        status: undefined,
        deptId: undefined,
        pageNum: 1,
        pageSize: 10
      }
      this.handleQuery()
    },
    onShowSizeChange (current, pageSize) {
      this.queryParam.pageSize = pageSize
      this.getList()
    },
    changeSize (current, pageSize) {
      this.queryParam.pageNum = current
      this.queryParam.pageSize = pageSize
      this.getList()
    },
    onSelectChange (selectedRowKeys, selectedRows) {
      this.selectedRowKeys = selectedRowKeys
      this.selectedRows = selectedRows
      this.ids = this.selectedRows.map(item => item.id)
      this.single = selectedRowKeys.length !== 1
      this.multiple = !selectedRowKeys.length
    },
    toggleAdvanced () {
      this.advanced = !this.advanced
    },
    /** 导出按钮操作 */
    handleExport () {
      var that = this
      this.$confirm({
        title: '是否确认导出?',
        content: '此操作将导出当前条件下所有数据而非选中数据',
        onOk () {
          that.download('manager/studentCard/incomeExport', {
            ...that.queryParam
          }, `收入结转.xlsx`)
        },
        onCancel () {}
      })
    }
  }
}
</script>
<style lang="less" scoped>
</style>
